package com.casic.modules.hcry.controller;

import java.util.List;
import java.util.Map;

import com.casic.common.utils.CommonUtil;
import com.casic.common.utils.Constant;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.validator.group.AddGroup;
import com.casic.common.validator.group.UpdateGroup;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysUserRoleService;
import com.casic.modules.sys.service.SysUserService;
import com.casic.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casic.modules.hcry.entity.QyxxDdRyhcEntity;
import com.casic.modules.hcry.service.QyxxDdRyhcService;


/**
 * 核查人员信息
 * 
 * @author ws
 * @email 123456789@qq.com
 * @date 2019-05-13 13:52:36
 */
@RestController
@RequestMapping("qyxxddryhc")
public class QyxxDdRyhcController {
	@Autowired
	private QyxxDdRyhcService qyxxDdRyhcService;

	@Autowired
	private SysUserRoleService sysUserRoleService;

	@Autowired
	private SysUserService sysUserService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("qyxxddryhc:list")
	public R list(@RequestParam Map<String, Object> params){
		List<String> roles = sysUserRoleService.findRoleIdByUserId(ShiroUtils.getUserEntity().getUserId());
		if(roles.size() == 1 && Constant.DDQY_ROLE_ID.equals(roles.get(0))) {
			params.put("ryhcQydm", ShiroUtils.getUserEntity().getUsername());
		}
		if(CommonUtil.isGongan(roles)) {
			params.put("deptId", ShiroUtils.getUserEntity().getDeptId());
		}
        PageEntity<QyxxDdRyhcEntity> page=qyxxDdRyhcService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("qyxxddryhc:info")
	public R info(@PathVariable("id") String id){
		QyxxDdRyhcEntity qyxxDdRyhc = qyxxDdRyhcService.findById(id);
		return R.ok().put("qyxxDdRyhc", qyxxDdRyhc);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("qyxxddryhc:save")
	public R save(@RequestBody QyxxDdRyhcEntity qyxxDdRyhc){
		String username = ShiroUtils.getUser().getUsername();
		SysUserEntity sysUserEntity = sysUserService.queryByUserName(username);
		// 如果典当企业管理员，设置机构号和所属企业名称、代码
		if(CommonUtil.isDdqy(sysUserEntity.getRoleIdList())) {
			qyxxDdRyhc.setRyhcQydm(sysUserEntity.getUsername());
			qyxxDdRyhc.setRyhcQymc(sysUserEntity.getNickName());
			qyxxDdRyhc.setDeptId(sysUserEntity.getDeptId());
		}
		qyxxDdRyhcService.save(qyxxDdRyhc);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("qyxxddryhc:update")
	public R update(@RequestBody QyxxDdRyhcEntity qyxxDdRyhc){
		qyxxDdRyhcService.edit(qyxxDdRyhc);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("qyxxddryhc:delete")
	public R delete(@RequestBody String[] ids){
		qyxxDdRyhcService.removeByIds(ids);
		return R.ok();
	}
	
}
